package com.zhaodui.restapi.base;


import com.google.gson.Gson;
import com.zhaodui.dhcar.entity.FxjDhCarWzdetailEntity;
import com.zhaodui.dhfk.entity.FxjDhfkLogEntity;
import com.zhaodui.jdwx.jdwxController;
import com.zhaodui.order.entity.FxjOrderRmEntity;
import com.zhaodui.restapi.entity.FkInputEntity;
import com.zhaodui.restapi.entity.fxj360wz;
import com.zhaodui.restapi.entity.fxjzcxcx;
import com.zhaodui.restapi.entity.jdwxerrres;
import com.zhaodui.util.fxjUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.jeecgframework.core.util.JSONHelper;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.xiaoleilu.hutool.date.DateTime.now;

/**
 * TSUser的Restful API
 * 
 *  path/ POST    --> 新增 (CREATE)
 *  path/id  GET -->  查询 (READ)
 *  path/id  PUT  -->  更新 (UPDATE)
 *  path/id  DELETE --> 删除 (DELETE)
 * 
 * @author liuht
 */
//update--begin--author:zhangjiaqiang date:20171030 for:增加swagger支持
@Api(value="fkrest",description="风控接口",tags="FkrestController")
//update--end--author:zhangjiaqiang date:20171030 for:增加swagger支持
@Controller
@RequestMapping(value = "/fk")
public class FkrestController {
	@Autowired
	SystemService systemService;
	/**
	 * 访问地址：http://localhost:8080/jeecg/rest/user/name/{id}
	 * @param fkname
	 * @return  dosmrz  {"result":{"idcard":"1234567","isok":false,"realname":"曹二众"},"reason":"成功","error_code":0}
	 */
	@RequestMapping(value = "/{fkname}", method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	//update--begin--author:zhangjiaqiang date:20171030 for:增加swagger支持
	@ApiOperation(value="根据fkname查找对应风控",notes="根据fkname查找对应风控",httpMethod="POST",produces="application/json")
	//update--end--author:zhangjiaqiang date:20171030 for:增加swagger支持
	public ResponseMessage<?> fkbyname(@ApiParam(required=true,name="fkname",value="fkname")@PathVariable("fkname") String fkname, @RequestBody FkInputEntity fkinput) {
		String result = null;
        FxjOrderRmEntity fxjOrderRmEntity = new FxjOrderRmEntity();

        System.out.print("fkinput.getImgfile()"+fkinput.getImgfile());
		com.zhaodui.jdwx.jdwxController jdwx = new jdwxController();
		HttpServletRequest request = null;
		if(fkname.equals("dosmrz")){//身份证实名认证
			try {
				result = jdwx.dosmrz(fkinput.getCardNo(),fkinput.getRealName(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		if(fkname.equals("dotongdun")){//同盾
			try {
				result = jdwx.dotongdun(fkinput.getRealName(),fkinput.getCardNo(),fkinput.getMobile(),fkinput.getKeyword(),fkinput.getBankCardNo(),request);

				try{
//					resEntity restongdun = JSONHelper.fromJsonToObject(result,resEntity.class);
					JSONObject  myJson = JSONObject.fromObject(result);
					JSONObject  model = JSONObject.fromObject(myJson.get("ANTIFRAUD_MODEL").toString());
					JSONObject  query = JSONObject.fromObject(myJson.get("ANTIFRAUD_INFOQUERY").toString());
					JSONObject  ANTIFRAUD = JSONObject.fromObject(myJson.get("ANTIFRAUD").toString());


					fxjOrderRmEntity.setRmScores(ANTIFRAUD.get("final_score").toString());//风控得分
					fxjOrderRmEntity.setRmView(result);
//					fxjOrderRmEntity.setRmBy1(Integer.toString(fxjzcxcx.getData().getIdentityScore()));
//					fxjOrderRmEntity.setRmBy2(Integer.toString(fxjzcxcx.getData().getPerformanceScore()));
//					fxjOrderRmEntity.setRmBy3(Integer.toString(fxjzcxcx.getData().getCreditScore()));
//					fxjOrderRmEntity.setRmBy4(Integer.toString(fxjzcxcx.getData().getBehaviorScore()));
//					fxjOrderRmEntity.setRmBy5(Integer.toString(fxjzcxcx.getData().getSocialScore()));
//					fxjOrderRmEntity.setRmBy6(fxjzcxcx.getData().getScoreRange());
					fxjOrderRmEntity.setRmBy7(ANTIFRAUD.get("final_decision").toString());
					fxjOrderRmEntity.setRmBy8(ANTIFRAUD.get("risk_items").toString());



				}catch (Exception e){

				}



			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dosfzsb")){//身份证识别
			//证件类型：1（一代身份证 ）2（身份证正面）3（身份证背面）4（临时身份证) 1001（香港永久性居民身份证）1005（澳门身份证）1031（台湾身份证正面）1032（台湾身份证背面 ）
			try {
				result = jdwx.dosfzsb(fkinput.getImgfile(),fkinput.getShiietype(),request);
				System.out.print(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dozjsb")){//证件识别
			//证件类型: 5(驾照)6(行驶证)7(军官证)9(中华人民共和国往来港澳通行证)10(台湾居民往来大陆通行证)11(大陆居民往来台湾通行证 )12(签证)13(护照)14(港澳居民来往内地通行证正面)15(港澳居民来往内地通行证背面)16(户口本)1000(居住证)1003(边民证照片页)1004(边民证个人信息页)1030(全民健康保险卡)			model.setQueryParams(queryMap);
			try {
				result = jdwx.dozjsb(fkinput.getImgfile(),fkinput.getShiietype(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dorldb")){//人脸对比
			try {
				result = jdwx.dorldb(fkinput.getCardNo(),fkinput.getRealName(),fkinput.getImgfile(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dogrss")){//个人涉诉查询
			//类别英文代码，执行公告-zxgg；失信公告-sxgg；裁判文书-cpws；法院公告-fygg；全类包含上述所有-all
			try {
				result = jdwx.dogrss(fkinput.getCardNo(),fkinput.getRealName(),"all",request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dogrsxhmd")){//失信被执行人黑名单
			try {
				result = jdwx.dogrsxhmd(fkinput.getCardNo(),fkinput.getRealName(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dogrhmd")){//个人黑名单多头借贷综合查询
			try {
				result = jdwx.dogrhmd(fkinput.getCardNo(),fkinput.getRealName(),fkinput.getMobile(),fkinput.getBankCardNo(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dop2p")){//p2p
			try {
				result = jdwx.dop2p(fkinput.getQuery_qq(),fkinput.getQuery_email(),fkinput.getMobile(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dop2pblack")){//网络借贷黑名单
			try {
				result = jdwx.dop2pblack(fkinput.getCardNo(),fkinput.getRealName(),fkinput.getMobile(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dozwsc")){//手机号码在网时长查询
			try {
				result = jdwx.dozwsc(fkinput.getMobile(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("doqysx")){//企业失信查询
			try {
				result = jdwx.doqysx(fkinput.getKeyword(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dogrsx")){//个人失信查询
			try {
				result = jdwx.dogrsx(fkinput.getRealName(),fkinput.getCardNo(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dofqz")){// 反欺诈识别被检测用户，一般为手机号
			try {
				result = jdwx.dofqz(fkinput.getMobile(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("domg")){//蜜罐数据
			try {
				result = jdwx.domg(fkinput.getRealName(),fkinput.getCardNo(),fkinput.getMobile(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		if(fkname.equals("dosjeys")){// 手机号机主二要素验证
			try {
				result = jdwx.dosjeys(fkinput.getRealName(),fkinput.getMobile(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		if(fkname.equals("doclwzcs")){// 获取车辆违章城市
			try {
				result = jdwx.doclwzcs(request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dovindetail")){// 获取VIN详情
			try {
				result = jdwx.dovindetail(fkinput.getFrameno(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}


		if(fkname.equals("do360wz")){// 获取车辆违章信息360
			try {
				result = jdwx.do360wz(fkinput.getCreateBy(),fkinput.getLsnum(),fkinput.getFrameno(),fkinput.getEngineno(),request);
				try{
					FxjDhfkLogEntity fxjDhfkLogEntity = new FxjDhfkLogEntity();
					fxjDhfkLogEntity.setCreateBy(fkinput.getCreateBy());
					fxjDhfkLogEntity.setFkName(fkname);
					fxjDhfkLogEntity.setFkResult(result);
					systemService.save(fxjDhfkLogEntity);
				}catch (Exception e){

				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			try{
				fxj360wz fxj360Wz = JSONHelper.fromJsonToObject(result,fxj360wz.class);
				try{
					for(com.zhaodui.restapi.entity.fxj360wz.ResultBean.DataBean dt:fxj360Wz.getResult().getData()){
						FxjDhCarWzdetailEntity fxjDhCarWzdetailEntity = new FxjDhCarWzdetailEntity();
						fxjDhCarWzdetailEntity.setCarNo(fxj360Wz.getResult().getCph());
						fxjDhCarWzdetailEntity.setCharge(Boolean.toString(fxj360Wz.isCharge()));
						fxjDhCarWzdetailEntity.setWzid(dt.getWfdm());
						fxjDhCarWzdetailEntity.setTime(dt.getWzsj());
						fxjDhCarWzdetailEntity.setBpmStatus(dt.getWzdd());
						fxjDhCarWzdetailEntity.setContent(dt.getWzxw());
						fxjDhCarWzdetailEntity.setPrice(Integer.toString(dt.getWzfk()));
						fxjDhCarWzdetailEntity.setScore(Integer.toString(dt.getWzjf()));
						try{
							fxjDhCarWzdetailEntity.setBy1(result);

						}catch (Exception e){

						}
						systemService.save(fxjDhCarWzdetailEntity);
					}
					fxjOrderRmEntity.setRmView("违章罚款合计:"+fxj360Wz.getResult().getWzjfhj()+"违章扣分合计"+fxj360Wz.getResult().getWzjfhj());
					fxjOrderRmEntity.setRmBy8("违章次数合计:"+fxj360Wz.getResult().getWzts());

				}catch (Exception e){

				}

				return  Result.success(fxj360Wz);
			}catch (Exception e){
				try{
					jdwxerrres jdwxerrres = JSONHelper.fromJsonToObject(result,jdwxerrres.class);
					return  Result.error(jdwxerrres.getMsg());
				}catch (Exception e1){

				}
			}

		}
		if(fkname.equals("doclwz")){// 获取车辆违章信息
			try {
				result = jdwx.doclwz(fkinput.getCreateBy(),fkinput.getCarorg(),fkinput.getLsprefix(),fkinput.getLsnum(),fkinput.getFrameno(),fkinput.getEngineno(),fkinput.getKeyword6(),fkinput.getKeyword7(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dosyqjl")){// 逾期记录
			try {
				result = jdwx.dosyqjl(fkinput.getRealName(),fkinput.getCardNo(),fkinput.getMobile(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dozxfqz")){// 置信反欺诈报告
			try {
				result = jdwx.dozxfqz(fkinput.getRealName(),fkinput.getCardNo(),fkinput.getMobile(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dosfzshibie")){// 身份证
			try {
				result = jdwx.doshenfenzheng(fkinput.getImgfile(),fkinput.getShiietype(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dorenlianduibi")){// 人脸对比
			try {
				result = jdwx.dorenlianduibi(fkinput.getImgfile(),fkinput.getRealName(),fkinput.getCardNo(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("dosms")){// 发送短信
			try {
				result = jdwx.dosms(fkinput.getMobile(),fkinput.getKeyword1(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("doalyyzzsb")){// 营业执照
			try {
				result = jdwx.doyyzzsb(fkinput.getImgfile(),fkinput.getShiietype(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("doalzzsfpsb")){// 增值税
			try {
				result = jdwx.dozzsfpsb(fkinput.getImgfile(),fkinput.getShiietype(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(fkname.equals("doalbdcsb")){// 房产证
			try {
				result = jdwx.doalbdcsb(fkinput.getImgfile(),fkinput.getShiietype(),request);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
// 中诚信
		if(fkname.equals("dozcx")){
			try {
				result = jdwx.dozcx(fkinput.getRealName(),fkinput.getCardNo(),fkinput.getMobile(), request);
				try{
					fxjzcxcx fxjzcxcx = JSONHelper.fromJsonToObject(result,fxjzcxcx.class);
					fxjOrderRmEntity.setRmScores(Integer.toString(fxjzcxcx.getData().getScore()));//风控得分
					fxjOrderRmEntity.setRmView(fxjzcxcx.getResMsg());
					fxjOrderRmEntity.setRmBy1(Integer.toString(fxjzcxcx.getData().getIdentityScore()));
					fxjOrderRmEntity.setRmBy2(Integer.toString(fxjzcxcx.getData().getPerformanceScore()));
					fxjOrderRmEntity.setRmBy3(Integer.toString(fxjzcxcx.getData().getCreditScore()));
					fxjOrderRmEntity.setRmBy4(Integer.toString(fxjzcxcx.getData().getBehaviorScore()));
					fxjOrderRmEntity.setRmBy5(Integer.toString(fxjzcxcx.getData().getSocialScore()));
					fxjOrderRmEntity.setRmBy6(fxjzcxcx.getData().getScoreRange());
					fxjOrderRmEntity.setRmBy7(fxjzcxcx.getData().getSuggestion());
					fxjOrderRmEntity.setRmBy8(fxjzcxcx.getData().getMostSeriousRiskInfo());



				}catch (Exception e){

				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}


		System.out.print("fkname+result"+fkname+"******************"+result);
			try{
				fxjOrderRmEntity.setOrderNo(fkinput.getOrderNo());
				fxjOrderRmEntity.setRmType(fxjUtil.getdictname("fkname",fkname));
				fxjOrderRmEntity.setRmContent(result);

				fxjOrderRmEntity.setCreateBy(fkinput.getCreateBy());
				fxjOrderRmEntity.setCreateDate(now());

				systemService.save(fxjOrderRmEntity);
			}catch (Exception e){

			}
        return Result.success(fxjOrderRmEntity);
	}

}
